package com.css.fxfzxczh.modules.instrumentIntensity.repository.impl;

import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.instrumentIntensity.entity.InstrumentIntensity;
import com.css.fxfzxczh.modules.instrumentIntensity.repository.InstrumentIntensityRepository;
import com.css.fxfzxczh.modules.remoteSenseData.vo.RemoteSenseVO;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:
 * @CreateTime: 2024/3/22 10:47
 * @Description:
 */
@Repository
public class InstrumentIntensityRepositoryImpl implements InstrumentIntensityRepository {
    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public Map<String, Object> getPage(int curPage, int pageSize, String xsemId) {
        StringBuilder sql = new StringBuilder("select * from xczh_instrument_intensity ");
        StringBuilder whereSql = new StringBuilder("where del_flag='"+ YNEnum.N+"' ");
        if(PlatformObjectUtils.isNotEmpty(xsemId)){
            whereSql.append(" and xsem_id = '"+xsemId+"'");
        }
        int firstIndex = (curPage - 1) * pageSize;
        int lastIndex = pageSize;
        StringBuilder limitSql = new StringBuilder(" limit " + lastIndex + " offset " + firstIndex);
        sql.append(whereSql);
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(InstrumentIntensity.class)).size();
        List<InstrumentIntensity> list = jdbcTemplate.query(sql.append(limitSql).toString(), new BeanPropertyRowMapper<>(InstrumentIntensity.class));
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", list);
        return map;
    }

    @Override
    public void batchSave(List<InstrumentIntensity> list) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(list.toArray());

        String sql = "INSERT INTO xczh_instrument_intensity " +
                "( id,number,station_name,station_longitude,station_latitude,epicenter_distance,epicenter_azimuth,pga_ew," +
                "pga_ns,pga_ud,pgv_ew,pgv_ns,pgv_ud,instrument_intensity,remark,xsem_id,del_flag,create_time,create_user,geom)" +
                "values (:id,:number,:stationName,:stationLongitude,:stationLatitude,:epicenterDistance,:epicenterAzimuth,:pgaEw," +
                ":pgaNs,:pgaUd,:pgvEw,:pgvNs,:pgvUd,:instrumentIntensity,:remark,:xsemId,:delFlag,:createTime,:createUser,ST_GeomFromText(:geom,4490))";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public void delete(List<String> ids) {
        String sql = "update xczh_instrument_intensity set del_flag = '" + YNEnum.Y.toString() + "'where id in (:ids)";
        Map map = new HashMap();
        map.put("ids", ids);
        namedParameterJdbcTemplate.update(sql, map);
    }

    @Override
    public List<InstrumentIntensity> getInstrumentIntensityList(String xsemId) {
        String sql = "select * from xczh_instrument_intensity where del_flag='"+ YNEnum.N+"' and xsem_id = '"+xsemId+"'";

        List<InstrumentIntensity> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(InstrumentIntensity.class));
        return list;
    }

    @Override
    public List<InstrumentIntensity> getTopFiveStation(String xsemId) {
        String sql = "SELECT station_name,epicenter_distance,pga_ew,pga_ns,pga_ud from xczh_instrument_intensity " +
                "where del_flag='"+ YNEnum.N+"' and xsem_id = '"+xsemId+"' ORDER BY GREATEST(pga_ew, pga_ns, pga_ud) DESC limit 5";

        List<InstrumentIntensity> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(InstrumentIntensity.class));

        return list;
    }
}
